﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Cashier.utils;
using System.Text.RegularExpressions;

namespace Cashier.help
{
    public partial class ConfigForm : Form
    {
        public ConfigForm()
        {
            InitializeComponent();
            this.InitialFormData();

            comboBox_light.SelectedIndex = 0;
            cbComPort.SelectedIndex = 4;
        }

        /// <summary>
        /// 初始化表单数据
        /// </summary>
        protected void InitialFormData()
        {
            try
            {
                textbox_stock.Text = Config.STOCK_WARING.ToString();
                textbox_posname.Text = Config.SHOP_NAME;

                if (Config.IS_POS_DEBT)
                    radioButton_debt_yes.Checked = true;
                else
                    radioButton_debt_no.Checked = true;

                if (Config.RIGHTS_BUTTON_STATE.Equals(radioButton_hidden.Tag.ToString()))
                    radioButton_hidden.Checked = true;
                else if (Config.RIGHTS_BUTTON_STATE.Equals(radioButton_disabled.Tag.ToString()))
                    radioButton_disabled.Checked = true;

                if (Config.TICKET_PRINT_PREVIEW)
                    radioButton_ticketPrintPreview_yes.Checked = true;
                else
                    radioButton_ticketPrintPreview_no.Checked = true;

                textbox_ticketPromotions.Text = Config.TICKET_PROMOTIONS;
            }
            catch (Exception ex)
            {
                LogsUtil.Record(ex);
            }
        }

        #region 控件事件

        /// <summary>
        /// 退出
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button_exit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        /// <summary>
        /// 保存
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button_ok_Click(object sender, EventArgs e)
        {
            try
            {
                String stock = textbox_stock.Text.Trim();
                String reg = @"^\+?[1-9][0-9]*$";
                if (!Regex.IsMatch(stock, reg))
                {
                    MessageBox.Show("库存告警数量必须是整数！");
                    return;
                }
                
                Config.SetStockWaring(stock);
                Config.SetShopName(textbox_posname.Text.Trim());
                Config.SetIsPosDebt(radioButton_debt_yes.Checked ? "true" : "false");
                Config.SetRightsButtonState(radioButton_disabled.Checked ? radioButton_disabled.Tag.ToString() : radioButton_hidden.Tag.ToString());
                Config.SetTicketPrintPreview(radioButton_ticketPrintPreview_yes.Checked ? "true" : "false");
                Config.SetTicketPromotions(textbox_ticketPromotions.Text.Trim());
                Config.SaveXml();
                MessageBox.Show("保存成功，下次重启软件生效！");
            }
            catch (Exception ex)
            {
                LogsUtil.Record(ex);
            }
        }

        /// <summary>
        /// 保存客显设置
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button_customdisplay_save_Click(object sender, EventArgs e)
        {
            CustomerDisplay disliay;
            try
            {
                disliay = new CustomerDisplay(cbComPort.Text, int.Parse(textBox2.Text), System.IO.Ports.StopBits.One.ToString(), 8);
                switch (comboBox_light.Text)
                {
                    case "单价":
                        disliay.DispiayType = CustomerDispiayType.Price;
                        break;
                    case "收款":
                        disliay.DispiayType = CustomerDispiayType.Recive;
                        break;
                    case "总计":
                        disliay.DispiayType = CustomerDispiayType.Total;
                        break;
                    case "找零":
                        disliay.DispiayType = CustomerDispiayType.Change;
                        break;
                }
                disliay.DisplayData(textBox1.Text);

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

        #endregion

        #region 重写方法

        /// <summary>
        /// 重写键盘事件
        /// </summary>
        /// <param name="msg"></param>
        /// <param name="keyData"></param>
        /// <returns></returns>
        protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {
            bool b = base.ProcessCmdKey(ref msg, keyData);
            switch (keyData)
            {
                case Keys.Escape:
                    this.Close();
                    break;
            }
            return b;
        }

        #endregion

        
    }
}
